// userPageC/pages/userPersonal/balanceList/balanceList.js
const app = getApp()
const WXAPI = require('../../../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '余额明细', // 导航栏 中间的标题
      textColor: '#000', // 标题颜色
      bgColor: '#fff', // 导航栏背景颜色
      isloading: false,
      isColor: true,
    },
    retrieveType:[
      {id: '',value:'全部'},
      {id: '6',value:'提现'},
      {id: '7',value:'转卖收入'},
      // {id: '2',value:'订单类型'},
    ],
    currentNavtab: '',
    assetType:'',
    type: '',
    pageNum: 1,
    pageSize: 10,
    isPull: true,
    lodingtype: '',
    billList:[],//账单列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },
  handleType(e){
    var id = e.currentTarget.dataset.id
    this.data.pageNum = 1
    this.setData({
      currentNavtab: id,
      pageNum: 1,
      billList:[],
      isPull: true,
    })
    this.getList()
  },
  //列表
  getList(){
    var data = {
      type: this.data.currentNavtab,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
    }
    WXAPI.balanceList(data,(res)=>{
      console.log('账单列表',res)
      const {size,total,records} = res.result
      var billList = this.data.billList
      if(billList.length == 0){
        billList = billList.concat(records)
      }else{
        billList.map(list =>{
          records.map(item=>{
            if(item.groupTime == list.groupTime){
              list.children = list.children.concat(item.children)
              return list.children
            }else{
              billList = billList.concat(item)
              return billList
            }
          })
          // console.log('item-1',item)
        })
      }
      let hash = {};
      billList = billList.reduce((preVal, curVal) => {
        console.log(' hash[curVal.groupTime]', hash[curVal.groupTime])
        hash[curVal.groupTime] ? '' : hash[curVal.groupTime] = true && preVal.push(curVal);
        return preVal
      }, [])
      console.log('账单列表',billList)
      if(this.data.pageNum >= Math.ceil(total / size)){
        console.log('没有分页+++++++++++')
          this.setData({
            lodingtype: 1,
            isPull: false
          })
      }else{
        this.setData({
          lodingtype: 0
        })
      }
      this.setData({
        billList: billList
      })
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  onReachBottom: function() {
    if(this.data.isPull==true){
        var pageNum = this.data.pageNum
        pageNum ++
        console.log("触底了",pageNum)
        this.setData({
          pageNum: pageNum
        });
        this.getList()
      
    }
    // this.getticketList()
  },


})